clear
import delimited using SAINC4_ALL_AREAS_1929_2023.csv
rename v9 year1929
keep geofips geoname linecode description unit year1929
drop if geoname=="United States"
keep if linecode==10|linecode==20|linecode==47
gen statefip = substr(geofips,3,2)
destring statefip, gen(statefipnum)

gen transfx = year1929 if linecode==47
destring transfx, replace force
bysort statefipnum: egen transf = max(transfx)

gen popx = year1929 if linecode==20
destring popx, replace force
bysort statefipnum: egen pop = max(popx)

gen persinc = year1929 if linecode==10
destring persinc, replace force

gen persinclesstransf = persinc-transf
gen persinclesstransfdollars = persinclesstransf*1000000

gen pcpersinclesstransfdollars = persinclesstransfdollars/pop
keep if linecode==10
keep geoname statefipnum pcpersinclesstransfdollars pop
rename pop pop1929
save stateincomepercapita.dta, replace
clear
use "justnormasylum.dta"

tostring cty_fips, gen(cty_fipstr)
gen statefip = substr(cty_fipstr, 1,1) if length(cty_fipstr)==4
replace statefip = substr(cty_fipstr, 1, 2) if length(cty_fipstr)==5
destring statefip, replace
gen  statefipnum=statefip
merge m:1 statefipnum using stateincomepercapita.dta
rename _merge mergestateincomepc
keep if mergestateincomepc==3

bysort statefipnum: egen totinst = count(cty_fips)
bysort statefipnum: egen totnorm = total(hasnormalschool)

save stateincomepercapitanorm.dta, replace
 
clear
import delimited using SAINC4_ALL_AREAS_1929_2023.csv
rename v67 year1987
keep geofips geoname linecode description unit year1987
drop if geoname=="United States"
keep if linecode==20
gen statefip = substr(geofips,3,2)
destring statefip, gen(statefipnum)

gen pop = year1987 if linecode==20
destring pop, replace force

keep geoname statefipnum pop
rename pop pop1987
save Pop1987.dta, replace

clear

use "degenrollempstaff_inst_8018.dta"
merge m:1 cty_fips using "justnormasylum.dta"

keep if year==1987

tostring cty_fips, gen(cty_fipstr)
gen statefipnum = substr(cty_fipstr, 1,1) if length(cty_fipstr)==4
replace statefipnum = substr(cty_fipstr, 1, 2) if length(cty_fipstr)==5
destring statefipnum, replace

#delimit ;
gen pubnonresearch = public==1 & (class1987=="Comprehensive I"|class1987=="Comprehensive II"|
class1987=="Doctorate-Granting I"|class1987=="Doctorate-Granting II"|class1987=="Liberal Arts I"|class1987=="Liberal Arts II");

#delimit cr
bysort statefipnum: egen totpubnonr = total(pubnonresearch)
tab totpubnonr

keep if public==1
egen tagstate = tag(statefipnum)
keep if tagstate==1
keep stabbr statefipnum totpubnonr

merge 1:m statefipnum using stateincomepercapitanorm.dta
rename _merge mergenonnormpub

merge m:1 statefipnum using Pop1987.dta
rename _merge mergepop1987

egen tagstate = tag(statefipnum)

gen hasasylum = hasnormalschool==0 & hasnormalorasylum==1

bysort statefipnum: egen sthasnorm = max(hasnormalschool)
bysort statefipnum: egen sthasasyl = max(hasasylum)

gen normandasy = sthasnorm==1 & sthasasyl==1

*Restrict to states with both normal schools and asylums

local vars totinst totnorm totpubnonr  
foreach x of local vars{
reghdfe `x' pcpersinclesstransfdollars if tagstate==1 & normandasy==1, vce(robust) noabsorb
su `x' if e(sample)==1, d
local meandv = r(mean)
su pcpersinclesstransfdollars if e(sample)==1, d 
local sdpc = r(sd)
outreg2 using statepcincome.xls, append excel dec(3) addstat(meandv, `meandv', sdpc, `sdpc') adec(3)
}

save inst1929pcincome.dta, replace

